package com.lz.ionicbase.view;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lz.ionicbase.R;
import com.lz.ionicbase.model.WorkListDetailXBean;
import com.lz.ionicbase.ui.fragment.event.MiddleEvent;
import com.lz.ionicbase.ui.fragment.event.SmallEvent;
import com.lz.ionicbase.ui.fragment.event.WeightEvent;
import com.lz.ionicbase.utils.Constant;
import com.lz.ionicbase.utils.SpUtils;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * 任柏全
 * work轮播图
 */

public class MyWorkPagerAdapter<T> extends PagerAdapter {
    private ViewPager mViewPager;
    private Context mContext;
    private List<WorkListDetailXBean> list;
    private long downTime;
    private int curretPosition;
    private RunnableTask runnableTask;
    private TextView itemPagerTitle;
    private int newposition;
    private int nnewposition;
    private List<View> indicatorView;

    public MyWorkPagerAdapter(Context context, ViewPager mViewPager) {
        this.mContext = context;
        this.mViewPager = mViewPager;
    }

    @Override
    public int getCount() {
        if (list == null || list.size() == 0) {
            return 0;
        } else if (list.size() == 1) {
            return 1;
        }
        return Integer.MAX_VALUE;
    }


    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }


    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container,  int position) {
        Log.i("2.21", list.toString());
        int index = list.size();
        position = position % index;
        Log.i("------","position---"+position);
        View view = LayoutInflater.from(mContext).inflate(R.layout.work_item_viewpager, null);
        TextView itemPagerTitle = (TextView) view.findViewById(R.id.item_pager_title);
        TextView pagerSmallNumber = (TextView) view.findViewById(R.id.pager_small_number);
        TextView pagerMiddleNumber = (TextView) view.findViewById(R.id.pager_middle_number);
        TextView pagerHeightNumber = (TextView) view.findViewById(R.id.pager_height_number);
        final RelativeLayout smalllayout = (RelativeLayout) view.findViewById(R.id.small_layout);
        final RelativeLayout middlelayout = (RelativeLayout) view.findViewById(R.id.middle_layout);
        final RelativeLayout weightlayout = (RelativeLayout) view.findViewById(R.id.weight_layout);
        itemPagerTitle.setText(list.get(position).getMc());
        pagerSmallNumber.setText(list.get(position).getGreen());
        pagerMiddleNumber.setText(list.get(position).getYellow());
        pagerHeightNumber.setText(list.get(position).getRed());
        container.addView(view);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.i("------","11111111111111111111");
                newposition = position;
                nnewposition = newposition%list.size();
                smalllayout.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                middlelayout.setEnabled(false);
                                weightlayout.setEnabled(false);
                                // 轮播图不再滚动,取消维护的任务,发送的消息
                                handler.removeCallbacksAndMessages(null);
                                // 1,按下的坐标点
                                downTime = System.currentTimeMillis();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                break;
                            case MotionEvent.ACTION_UP:
                                //  给Fragment传递信息 代替点击事件
                                if (System.currentTimeMillis() - downTime < 500) {
                                    EventBus.getDefault().post(new SmallEvent(list.get(nnewposition).getSgreenurl()));
                                }
                                startRoll();
                                middlelayout.setEnabled(false);
                                weightlayout.setEnabled(false);

                                break;
                            case MotionEvent.ACTION_CANCEL:
                                startRoll();
                                break;
                        }
                        return false;
                    }
                });
                middlelayout.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                smalllayout.setEnabled(false);
                                weightlayout.setEnabled(false);
                                // 轮播图不再滚动,取消维护的任务,发送的消息
                                handler.removeCallbacksAndMessages(null);
                                // 1,按下的坐标点
                                downTime = System.currentTimeMillis();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                break;
                            case MotionEvent.ACTION_UP:
//                         让轮播图再次滚动起来
                                if (System.currentTimeMillis() - downTime < 500) {
                                    EventBus.getDefault().post(new MiddleEvent(list.get(nnewposition).getSyellowurl()));
                                }
                                startRoll();
                                smalllayout.setEnabled(true);
                                weightlayout.setEnabled(true);
                                break;
                            case MotionEvent.ACTION_CANCEL:
                                startRoll();
                                break;
                        }
                        return false;
                    }
                });
                weightlayout.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        switch (event.getAction()) {
                            case MotionEvent.ACTION_DOWN:
                                middlelayout.setEnabled(false);
                                smalllayout.setEnabled(false);
                                // 轮播图不再滚动,取消维护的任务,发送的消息
                                handler.removeCallbacksAndMessages(null);
                                // 1,按下的坐标点
                                downTime = System.currentTimeMillis();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                break;
                            case MotionEvent.ACTION_UP:
//                         让轮播图再次滚动起来
                                if (System.currentTimeMillis() - downTime < 500) {
                                    EventBus.getDefault().post(new WeightEvent(list.get(nnewposition).getSredurl()));
                                }
                                startRoll();
                                middlelayout.setEnabled(true);
                                smalllayout.setEnabled(true);
                                break;
                            case MotionEvent.ACTION_CANCEL:
                                startRoll();
                                break;
                        }
                        return false;
                    }
                });
            }

            @Override
            public void onPageSelected( int position) {
                Log.i("------", "3333333333333333");
                if (list.size() != 0) {
                    int index = position % list.size();
                    for (int i = 0; i < list.size(); i++) {
                        View view = indicatorView.get(i);
                        view.setBackgroundResource(index == i? R.drawable.selected_point_shape : R.drawable.unselected_point_shape);
                    }
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.i("------", "22222222222222222222");
            }
        });

        return view;
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            // 让viewpager按照维护的索引去指向界面
            mViewPager.setCurrentItem(newposition);
            // 一直去维护runnable任务去滚动起来
            startRoll();
        }
    };

    public void startRoll() {
        notifyDataSetChanged();
        // 2,滚动起来(1,定时器2,handler)
        handler.postDelayed(runnableTask, 3000);
    }

    public void setIndicator(List<View> indicatorView) {
        this.indicatorView = indicatorView;
    }

    class RunnableTask implements Runnable {
        @Override
        public void run() {
            newposition = mViewPager.getCurrentItem() + 1;
            // 发送消息api
            handler.obtainMessage().sendToTarget();
        }
    }

    public void setList(List<WorkListDetailXBean> list) {
        this.list = list;
        notifyDataSetChanged();
        if (list != null && list.size() > 0 && runnableTask == null) {
            runnableTask = new RunnableTask();
            SpUtils.saveBoolean(mContext, Constant.ISHANDLER, true);
            startRoll();
        } else if (runnableTask != null) {
            SpUtils.saveBoolean(mContext, Constant.ISHANDLER, false);
            startRoll();
        }
    }

    public void cancleHandle() {
        handler.removeCallbacksAndMessages(null);
    }

    public int getPosition() {
        int currentPosition = newposition;
        return currentPosition;
    }
}
